// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import App from "./App";
import router from "./router";
import filters from "./utils/filter"; // 全局过滤器
import i18n from "./lang";
import storage from "storejs";

// import VConsole from "vconsole"
// new VConsole()

const FastClick = require("fastclick");
document.addEventListener(
  "DOMContentLoaded",
  function() {
    FastClick.attach(document.body);
  },
  false
);

import { Layout } from "vue-ydui/dist/lib.rem/layout";
Vue.component(Layout.name, Layout);

import { CountDown } from "vue-ydui/dist/lib.rem/countdown";
Vue.component(CountDown.name, CountDown);

import { PullRefresh } from "vue-ydui/dist/lib.rem/pullrefresh";
Vue.component(PullRefresh.name, PullRefresh);

import { Popup } from "vue-ydui/dist/lib.rem/popup";
Vue.component(Popup.name, Popup);

import { CellGroup, CellItem } from "vue-ydui/dist/lib.rem/cell";
Vue.component(CellGroup.name, CellGroup);
Vue.component(CellItem.name, CellItem);

Vue.config.productionTip = false;

Vue.prototype.$storage = storage; // 赋予Vue原型上能够直接获取本地存储封装好的能力

for (let key in filters) {
  // 循环加载所有的全局过滤器
  filters[key](Vue);
}

// 取消页面的长按事件
// window.ontouchstart = function(e) { e.preventDefault(); };

window.createcode = function(cb) {
  $.ajax({
    type: "get",
    url: "/public/wx/wechat/createImgValidate",
    dataType: "json",
    success: function({ data }) {
      console.log(data, $("#imgscode"));
      if (data != null) {
        $("#imgscode").imgcode({
          frontimg: "data:image/png;base64," + data.slidingImage,
          backimg: "data:image/png;base64," + data.backImage,
          yHeight: data.yHeight,
          refreshcallback: function() {
            //刷新验证码
            window.createcode();
          },
          callback: function(msg) {
            var $this = this;
            $.ajax({
              type: "POST",
              url: "/public/wx/wechat/checkcapcode",
              data: {
                xpos: msg.xpos,
                capcode: data.capcode,
              },
              dataType: "json",
              success: function(data) {
                if (data.code == 1) {
                  $this.getsuccess();
                  setTimeout(function() {
                    cb();
                  }, 800);
                } else {
                  if (data.code == 4) {
                    window.createcode();
                  } else if (data.code == 3) {
                    $this.getfail("验证码过期,请刷新");
                  } else {
                    $this.getfail("验证不通过");
                  }
                }
              },
            });
          },
        });
      }
    },
    error: function(err) {
      console.log(err);
    },
  });
};

/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  i18n, // 多语言
  render: (h) => h(App),
});
